@echo off&&SETLOCAL ENABLEDELAYEDEXPANSION

set Save=%cd%

if "%1" == "" (
    echo input dir
    goto __EXIT
)
echo.
echo ----------------------------------------------------
echo PATH: %Save%
set version_dir=%1

set compile_ver_h_file=app_version.h

set compile_name=%2
set compile_ver=
set compile_hash=

set version_file=%version_dir%/version.txt

echo compile name: %compile_name%

:__CHECK_VERSION_FILE
if not exist %version_file% (
    set compile_ver=1
    goto _VER_CHECK
)


:__GET_VER_STR
for /f %%a in (%version_file%) do (
    set compile_ver=%%a
    echo version: %%a
    goto __SET_COMPILE_TIME
)

:__SET_COMPILE_TIME
set "__VBS__=%temp%\Spring.vbs"
>%__VBS__% echo dt=now()
>>%__VBS__% echo s_date=right(year(dt),4) ^& "-" ^& right("0" ^& month(dt),2) ^& "-" ^& right("0" ^& day(dt),2)
>>%__VBS__% echo s_time=right("0" ^& hour(dt),2) ^& ":" ^& right("0" ^& minute(dt),2) ^& ":" ^& right("0" ^& second(dt),2)
>>%__VBS__% echo wscript.echo(s_date^&"T"^&s_time)
for /f %%a in ('cscript /nologo "%__VBS__%"') do (
    set __DATE__=%%a
)
del /f /q %__VBS__%

echo compile time: %__DATE__%

echo #ifndef __APP_VERSION_H__                              >  %compile_ver_h_file%
echo #define __APP_VERSION_H__                              >> %compile_ver_h_file%
echo.                                                       >> %compile_ver_h_file%
echo #define COMPILE_PROJECT_NAME       "%compile_name%"    >> %compile_ver_h_file%
echo #define COMPILE_VERSION_STRING     "%compile_ver%"     >> %compile_ver_h_file%
echo #define COMPILE_TIME               "%__DATE__%"        >> %compile_ver_h_file%
echo #define COMPILE_HASH               "%compile_hash%"    >> %compile_ver_h_file%
echo.                                                       >> %compile_ver_h_file%
echo #endif                                                 >> %compile_ver_h_file%
echo ----------------------------------------------------
echo.
echo.
echo.

:__EXIT
